package com.baizhi.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/validateCode")
public class ValidateCodeController extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //生成验证码
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);

        //将真实验证码保存到session作用域中
        String code = lineCaptcha.getCode();
        System.out.println("code = " + code);
        HttpSession session = req.getSession();
        session.setAttribute("realCode",code);

        //将验证码响应回浏览器
        ServletOutputStream out = resp.getOutputStream();
        lineCaptcha.write(out);
    }
}
